﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DataAccess;
using DevExpress.Web.ASPxEditors;
using Microsoft.VisualBasic;

namespace Web.Business
{
    public partial class ComboBoxBal
    {
        #region | Application |

        public static void BindApplList(
            System.Web.UI.Page page,
            ASPxComboBox cbx,
            String companyCode,
            String value = null,
            String text = null,
            bool showAll = true)
        {
            #region | Anonymous Type |

            //Anonymous Type => T.
            var type = new
            {
                applNo = new String(' ', 3) //=> default empty and max length string 3 digit.
                          ,
                applName = new String(' ', 100) //=> default empty and max length string 100 digit.
            };

            #endregion

            // bring collection of anonymous type items from a method!.
            var obj = ApplicationBal.GetInstance()
                                                    .GetApplAnonymousT(
                                                                page: page,
                                                                type: type, //=> passed type out set.
                                                                companyCode: companyCode
                                                                ).AsEnumerable();
            if (obj != null)
                if (obj.Count() > 0)
                {
                    cbx.Items.Clear(); //=> Clear Items.

                   foreach (var item in obj)
                           cbx.Items.Add(new ListEditItem(
                                  text: item.applName,
                                  value: item.applNo));

                   // Setting Controls
                   SetControlItems(
                               page: page,
                               cbx: cbx,
                               value: value,
                               text: text,
                               showAll: showAll);
                }
        }

        #endregion

        #region | Function Application |

        public static void BindFuncApplList(
            System.Web.UI.Page page,
            ASPxComboBox cbx,
            String appNo,
            String companyCode,
            String value = null,
            String text = null,
            bool showAll = true)
        {

            #region | Anonymous Type |

            //Anonymous Type => T.
            var type = new
                          {
                            funcNo = new String(' ', 5) //=> default empty and max length string 5 digit.
                                        ,
                            funcName = new String(' ', 100) //=> default empty and max length string 100 digit.
                          };

            #endregion

            // bring collection of anonymous type items from a method!.
            var obj = FunctionApplicationBal.GetInstance()
                                                    .GetFuncAnonymousT(
                                                                page: page,
                                                                type: type, //=> passed type out set.
                                                                companyCode: companyCode,
                                                                applNo: appNo).AsEnumerable();

            if(obj != null)
                if (obj.Count() > 0)
                {
                    cbx.Items.Clear(); //=> Clear Items.

                    foreach (var item in obj)
                        cbx.Items.Add(new ListEditItem(
                                                text: item.funcName,
                                                value: item.funcNo));
                    // Setting Controls
                    SetControlItems(
                                page: page,
                                cbx: cbx,
                                value: value,
                                text: text,
                                showAll: showAll);

                }
        }

        #endregion

        #region | Redundant Control Arguments |

        private static void SetControlItems(
            System.Web.UI.Page page,
            ASPxComboBox cbx,
            String value = null,
            String text = null,
            bool showAll = true)
        {
            try
            {
                if (showAll) //=> Binding Default SelectedItem.Index => 0.
                    cbx.Items.Insert(0, new ListEditItem(
                                            text: Core.ResourceTools
                                                            .GetResource(
                                                                    resourceKey: "SelectPlease"),
                                            value: string.Empty));


                if (!String.IsNullOrEmpty(value) || !String.IsNullOrEmpty(text))
                {
                    if (!String.IsNullOrEmpty(value)) cbx.SelectedItem.Value = value;

                    if (!String.IsNullOrEmpty(text)) cbx.SelectedItem.Text = text;
                }
                else
                    cbx.SelectedItem.Index = 0; // => Setting First Index item => 0.
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex));
                //
            }
        }

        #endregion


    }
}